package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.SkillItemsSet;
import com.xmy.cultivate.entity.SkillShutnum;
import com.xmy.cultivate.entity.TrainPrepare;
import com.xmy.cultivate.entity.TrainPrepareDetail;
import com.xmy.cultivate.service.ITrainPrepareDetailService;
import com.xmy.cultivate.service.ITrainPrepareService;

import com.xmy.cultivate.service.ITrainStudentVideoService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 教师备课资料设置 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-08-16
 */
@RestController
@RequestMapping("/api/trainPrepare")
public class TrainPrepareController extends BaseController{

    @Autowired
    private ITrainPrepareService iTrainPrepareService;
    @Autowired
    private ITrainPrepareDetailService iTrainPrepareDetailService;


    @GetMapping("/findAll")
    public Result findAll(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                          @RequestParam(value = "yearPart", required = false) String yearPart,
                          @RequestParam(value = "quarterNum", required = false) String quarterNum,
                          @RequestParam(value = "yearClassId", required = false) String yearClassId,
                          @RequestParam(value = "subjectsId", required = false) String subjectsId,
                          @RequestParam(value = "classTypeId", required = false) String classTypeId ,
                          @RequestParam(value = "lessonType", required = false) String lessonType,
                          @RequestParam(value = "versionId", required = false) String versionId
    ) {
        IPage<TrainPrepare> iPage = new Page<>(pageNum, pageSize);
        iTrainPrepareService.findAll(iPage, yearPart, quarterNum, yearClassId, subjectsId, classTypeId,lessonType,versionId);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));

    }

    @PostMapping("/addTrainPrepare")
    public Result addTrainPrepare(@RequestBody TrainPrepare trainPrepare) {
        QueryWrapper<TrainPrepare> qw = new QueryWrapper<>();
        qw.eq("year_part", trainPrepare.getYearPart());
        qw.eq("quarter_num", trainPrepare.getQuarterNum());
        qw.eq("year_class_id", trainPrepare.getYearClassId());
        qw.eq("subjects_id", trainPrepare.getSubjectsId());
        qw.eq("lesson_type", trainPrepare.getLessonType());
        qw.eq("class_type_id", trainPrepare.getClassTypeId());
        qw.eq("version_id", trainPrepare.getVersionId());
        qw.eq("deleted", 0);
        long count = iTrainPrepareService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据已存在");
        }

        boolean save = iTrainPrepareService.save(trainPrepare);
        if (!save) {
            return new Result(Code.ERROR, null, "添加失败");
        }
        return new Result(Code.OK, null, "添加成功");
    }

    @PutMapping("/editTrainPrepare")
    public Result editTrainPrepare(@RequestBody TrainPrepare trainPrepare) {
        TrainPrepare trainPrepareOld = iTrainPrepareService.getById(trainPrepare.getId());
        if (Objects.isNull(trainPrepareOld)) {
            return new Result(Code.ERROR, null, "该数据不存在,编辑失败");
        }
        QueryWrapper<TrainPrepare> qw = new QueryWrapper<>();
        qw.ne("id", trainPrepare.getId());
        qw.eq("year_part", trainPrepare.getYearPart());
        qw.eq("quarter_num", trainPrepare.getQuarterNum());
        qw.eq("year_class_id", trainPrepare.getYearClassId());
        qw.eq("subjects_id", trainPrepare.getSubjectsId());
        qw.eq("class_type_id", trainPrepare.getClassTypeId());
        qw.eq("lesson_type", trainPrepare.getLessonType());
        qw.eq("version_id", trainPrepare.getVersionId());
        qw.eq("deleted", 0);
        long count = iTrainPrepareService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据已存在");
        }
        boolean update = iTrainPrepareService.updateById(trainPrepare);
        if (!update) {
            return new Result(Code.ERROR, null, "修改失败");
        }
        return new Result(Code.OK, null, "修改成功");
    }

    @DeleteMapping("/deleteTrainPrepare")
    public Result deleteTrainPrepare(@RequestBody TrainPrepare trainPrepare) {
        TrainPrepare trainPrepareOld = iTrainPrepareService.getById(trainPrepare.getId());
        if (Objects.isNull(trainPrepareOld)) {
            return new Result(Code.ERROR, null, "该数据不存在,删除失败");
        }
        QueryWrapper<TrainPrepareDetail> qw = new QueryWrapper<>();
        qw.eq("prepare_id", trainPrepare.getId());
        long count = iTrainPrepareDetailService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据有课次,请先删除课次");
        }
        boolean delete = iTrainPrepareService.removeById(trainPrepare);
        if (!delete) {
            return new Result(Code.ERROR, null, "删除失败");
        }
        return new Result(Code.OK, null, "删除成功");
    }


}
